<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Clubs.php 2010-09-07 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Like_Model_DbTable_Clubs extends Engine_Db_Table
{
  protected $_name = 'like_clubs';

  protected $_rowClass = 'Like_Model_Club';
  
  public function getMutualFriendsAmongLikesSelect($params, $user_id = 0)
  {
  	if (!$user_id){
  		$user_id = Engine_Api::_()->user()->getViewer()->getIdentity();
  	}
  	
  	$likeTable = Engine_Api::_()->getDbTable('likes', 'like');
  	$name0 = $likeTable->info('name');
  	
  	$userTable = Engine_Api::_()->getItemTable('user');
  	$name1 = $userTable->info('name');
  	
  	$select = $userTable->select()->setIntegrityCheck(false);
  		
  	if (!empty($params['count']) && $params['count']){
  		$select
  			->from($name1, array('count' => 'COUNT(*)'))
  			->group($name0.'.like_club_id');
  	}else{
  		$select
  			->from($name1);
  	}
  	
  	$membershipTable = Engine_Api::_()->getDbTable('membership', 'user');
  	$name2 = $membershipTable->info('name');
  	
  	$select
  		->joinLeft($name0, $name0.'.user_id = '.$name1.'.user_id')
  		->joinLeft($name2, $name1.'.user_id = '.$name2.'.user_id')
  		->where($name2.'.resource_id = ?', $user_id)
  		->where($name2.'.resource_approved = 1')
  		->where($name2.'.user_approved = 1');
  		
  	if (!empty($params['club_id'])){
  		$select
  			->where($name0.'.like_club_id = ?', $params['club_id']);
  	}
  	
  	if (!empty($params['keyword'])){
  		$select
  			->where($name1.".username LIKE '%{$params['keyword']}%' OR ".$name1.".displayname LIKE '%{$params['keyword']}%'");
  	}
  	
  	return $select;
  }
  
  public function getMutualFriendsAmongLikesPaginator($params, $user_id = 0)
  {
  	if (!$user_id){
  		$user_id = Engine_Api::_()->user()->getViewer()->getIdentity();
  	}
  	
  	$select = $this->getMutualFriendsAmongLikesSelect($params, $user_id);
  	$paginator = Zend_Paginator::factory($select);
  	if (!empty($params['ipp'])){
  		$paginator->setItemCountPerPage($params['ipp']);
  	}
  	
  	if (!empty($params['p'])){
  		$paginator->setItemCountPerPage($params['p']);
  	}
  	 
  	return $paginator;
  }

  public function getMutualFriendsAmongLikes($params, $user_id = 0)
  {	
  	if (!$user_id){
  		$user_id = Engine_Api::_()->user()->getViewer()->getIdentity();
  	}
  	
  	if (!empty($params['count']) && $params['count']){
  		return $this->getAdapter()->fetchOne($this->getMutualFriendsAmongLikesSelect($params, $user_id));
  	}
  	
  	return $this->getMutualFriendsAmongLikesPaginator($params, $user_id);
  } 
  
  public function getRandomLikeClub()
  {
    $viewer_id = Engine_Api::_()->user()->getViewer()->getIdentity();
    
    $clubTableName = $this->info('name');
    $likeUserTableName = Engine_Api::_()->getDbTable('likes', 'like')->info('name');
    
    $select = $this->select()
      ->setIntegrityCheck(FALSE)
      ->from($clubTableName, array('club_id'))
      ->joinLeft($likeUserTableName, $likeUserTableName.'.like_club_id = '.$clubTableName.'.club_id', array())
      ->where($likeUserTableName.'.user_id = '.$viewer_id.' OR '.$clubTableName.'.user_id = '.$viewer_id);
    
    $club_ids = $this->getAdapter()->fetchCol($select);
    $where = "";
    
    if (!empty($club_ids)){
      $where = $clubTableName.".club_id NOT IN (".implode(",", $club_ids).")";
    }
    
    $select = $this->select()
      ->order('RAND()')
      ->where('active = 1')
      ->limit(1);

    if ($where){
      $select
        ->where($where);
    }
    
  	return $this->fetchRow($select);
  }
  
  public function getPaginator($params = array(), $cols = array('object', 'object_id'))
  {
  	$select = $this->getSelect($params, $cols);
  	$paginator = Zend_Paginator::factory($select);
  	
  	if (!empty($params['ipp'])){
  		$paginator->setItemCountPerPage($params['ipp']);
  	}
  	
    if (!empty($params['p'])){
      $paginator->setCurrentPageNumber($params['p']);
    }
    
    return $paginator;
  }
  
  public function getSelect($params = array(), $cols = array('object', 'object_id'))
  {
    $select = $this->select();
    $name0 = $this->info('name');
    
    $select
     ->setIntegrityCheck(false)
     ->from(array('c' => $name0), $cols)
     ->where('c.active = 1');
     
    $userTable = Engine_Api::_()->getDbTable('likes', 'like');
    $name1 = $userTable->info('name');
    
    if (!empty($params['like_id'])){
      $select
       ->joinLeft(array( 'like1' => $name1), 'like1.like_club_id = c.club_id', array())
       ->where("like1.user_id = {$params['like_id']}");
    }
    
    if (!empty($params['object'])){
      $select
       ->where("c.object = '{$params['object']}'");
    }
    
    if (!empty($params['object_id'])){
      $select
       ->where("c.object_id = '{$params['object_id']}'");
    }

    if (!empty($params['limit'])){
      $select
       ->limit($params['limit']);
    }

    if (!empty($params['mutual'])){
      $select
       ->joinLeft(array('like2' => $name1), 'like2.like_club_id = c.club_id', array())
       ->where("like2.user_id = {$params['mutual']}");
    }
     
    return $select;
  }
  
  public function getLikeClub($params = array())
  {
    return $this->fetchRow($this->getSelect($params));
  }
  
  public function getLikeClubObjectsByType($params, $type)
  {
    if (!Engine_Api::_()->getDbTable('modules', 'core')->isModuleEnabled($type)){
      return array();
    }
    
  	$cols = array('object_id');
  	$params['object'] = $type;
  	$data = $this->getAdapter()->fetchCol($this->getSelect($params, $cols));

    if (empty($data)){
      return array();
    }

    $table = Engine_Api::_()->getItemTable($type);
    $primary = $table->info('primary');
    $primary = $primary[1];
    $where = $primary . " IN (" . implode(",", $data) . ")";
    $select = $table->select()->where($where);

    $paginator = Zend_Paginator::factory($select);

    if (!empty($params['ipp'])){
      $paginator->setItemCountPerPage($params['ipp']);
    }

    if (!empty($params['p'])){
      $paginator->setCurrentPageNumber($params['p']);
    }

    return $paginator;
  }
  
  public function getLikeClubObjects($params = array())
  {
  	$objects = array();
  	$objects['users'] = $this->getLikeClubObjectsByType($params, 'user');
  	$objects['pages'] = $this->getLikeClubObjectsByType($params, 'page');
  	
  	return $objects;
  }
  
  public function getLikeClubs($params = array())
  {
  	return $this->getPaginator($params);
  }
}